package com.fsts.client;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JTextArea;

/**
 * 验证线程处理类
 * 
 * @desc 该类用于处理验证线程与对方的一些交互功能
 * @version 1.0
 * @since JDK1.6(建议)
 */
public class ValidateIdentityHandler implements Runnable {
	private Socket s;
	private JTextArea infoTextArea;
	private JButton recieveFileButton;
	private List<ValidateIdentityHandler> handlers;

	public ValidateIdentityHandler(Socket s, JTextArea infoTextArea,
			JButton recieveFileButton) {
		this.s = s;
		this.infoTextArea = infoTextArea;
		this.recieveFileButton = recieveFileButton;
	}

	public List<ValidateIdentityHandler> getHandlers() {
		return handlers;
	}

	public void setHandlers(List<ValidateIdentityHandler> handlers) {
		this.handlers = handlers;
	}

	public void run() {
		DataInputStream in = null;
		DataOutputStream outss = null;
		try {
			in = new DataInputStream(s.getInputStream());
			outss = new DataOutputStream(s.getOutputStream());
			final DataOutputStream outs = outss;
			while (true) {
				String req = in.readUTF();
				if (req.indexOf("sendFileRequest") >= 0) {
					infoTextArea.append("Bob给你发送文件，是否接受?\n");
					recieveFileButton
							.addActionListener(new java.awt.event.ActionListener() {
								public void actionPerformed(
										java.awt.event.ActionEvent evt) {
									try {
										outs.writeUTF("0");
									} catch (IOException e) {
										e.printStackTrace();
									}
								}
							});
					outs.flush();
					continue;
				}
				break;
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				in.close();
				outss.close();
				s.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

}